package com.example.dllo.my36kr.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.dllo.my36kr.R;
import com.example.dllo.my36kr.model.AcademyBean;
import com.squareup.picasso.Picasso;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by CPC_272 on 16/8/25.
 */
public class AcademyAdapter extends BaseAdapter {
     private Context context;
     private List<AcademyBean.DataBean.DataBean1> datas;

    public AcademyAdapter(Context context) {
        this.context = context;
    }

    public void setDatas(List<AcademyBean.DataBean.DataBean1> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas!=null?datas.size():0;
    }

    @Override
    public Object getItem(int position) {
        return datas!=null?datas.get(position):0;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_academy, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.academy_title.setText(datas.get(position).getTitle());
        holder.academy_author.setText(datas.get(position).getUser().getName());
        Long time = datas.get(position).getPublishTime();
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        String formatTime = format.format(new Date(time));
        holder.academy_time.setText(formatTime);
        Picasso.with(context).load(datas.get(position).getFeatureImg()).into(holder.academy_img);
        return convertView;
    }
    class ViewHolder {
        private ImageView academy_img;
        private TextView academy_title, academy_author, academy_time;

        public ViewHolder(View view) {
            academy_img = (ImageView) view.findViewById(R.id.academy_img);
            academy_title = (TextView) view.findViewById(R.id.academy_title);
            academy_author = (TextView) view.findViewById(R.id.academy_author);
            academy_time = (TextView) view.findViewById(R.id.academy_time);
        }
    }
}
